package com.ecs.core.other.state;
import com.artemis.Entity;
import com.ecs.core.components.attribute.MovementComp;
import com.ecs.core.components.draw.AnimationCom;
//import com.ecs.core.debug.Debug;
import com.ecs.core.system.StateSystem;
import com.ecs.core.utils.AnimationType;

public class MoveState implements IEntityState
{

	public static final String TAG = "MoveState";
	@Override
	public void enter(Entity e)
	{
	}

	@Override
	public void update(Entity e) {
		MovementComp movement = e.getComponent(MovementComp.class);
		AnimationCom animation = e.getComponent(AnimationCom.class);
		
		//移动时根据角度调整动画
		if(movement != null){
			float angle = movement.currentAngle;
			String direction = getFourDirectionAnimation(angle);
			
			if (animation != null) {
				if(!animation.currentAnimation.equals(direction)){
					animation.currentAnimation = direction;
					//Debug.log(TAG,String.format("Entity %d Current animation %s",e.getId(),direction));
				}
			}
			
			
			if(movement.velocity.isZero()){
				StateSystem.changeState(e,IdleState.class);
			}
		}
		
	}
	
	@Override
	public void exit(Entity e)
	{
		//Debug.log(TAG,String.format("Entity %d exit MoveState",e.getId()));
	}

	/**
	 * 根据角度返回 4 方向动画名称（0° 为正上方）
	 * @param angle 当前角度（0° 为正上方，顺时针增加）
	 * @return "up", "right", "down", "left"
	 */
	private String getFourDirectionAnimation(float normalizedAngle) {
		if (normalizedAngle >= 45 && normalizedAngle < 135) {
			return AnimationType.MOVERIGHT;  // 45°~135° → 右
		} else if (normalizedAngle >= 135 && normalizedAngle < 225) {
			return AnimationType.MOVEDOWN;   // 135°~225° → 下
		} else if (normalizedAngle >= 225 && normalizedAngle < 315) {
			return AnimationType.MOVELEFT;   // 225°~315° → 左
		} else {
			return AnimationType.MOVEUP;     // 315°~45° → 上
		}
	}


}
